home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Processes / MP Threaded Sort / Sort / ShellSort.cp < prev    next >
Encoding:
Text File  |  1997-03-13  |  1.5 KB  |  63 lines  |  [TEXT/MPS ]

  1. /*************************************************************************************
  2.  *
  3.  *    Apply a ShellSort algorithm to an offscreen, scrambled GWorld
  4.  *
  5.  *    ShellSort.cp    -    C Source
  6.  *
  7.  *      Copyright © Apple Computer, Inc. 1988 - 1993
  8.  *      All rights reserved.
  9.  *
  10.  *    This is the "guts" of the SortPicts program.  This file performs the actual
  11.  *      sort algorithm which gives the image its restoration quality (restoring itself
  12.  *       to its original self).
  13.  *
  14.  *************************************************************************************/
  15. #include "SortPicts.h"
  16.  
  17. void    ShellSort( SortPicts *sortPicts)
  18. {
  19.     sortPicts->ShellSort();
  20. }
  21.  
  22. /*************************************************************/
  23. /*                                                           */
  24. /*               The Actual Sort Algorithms                  */
  25. /*                                                           */
  26. /*************************************************************/
  27.  
  28.  
  29. void    SortPicts :: ShellSort( void)
  30. {
  31.     long            loop, sortSize, inLoop;
  32.     long            min;
  33.     long            data;
  34.  
  35.     sortSize = 1;
  36.     while( sortSize < N)
  37.         sortSize = 3 * sortSize + 1;
  38.     
  39.     do
  40.     {
  41.         sortSize /= 3;
  42.         for( loop = sortSize; loop < N; ++loop)
  43.         {
  44.             min = sortData[loop];
  45.  
  46.             for( inLoop = loop; (data = sortData[inLoop - sortSize]) > min; )
  47.             {
  48.                 SetSortItem( inLoop, data);
  49.  
  50.                 inLoop -= sortSize;
  51.                 if( inLoop < sortSize)
  52.                     break;
  53.             }
  54.             
  55.         SetSortItem( inLoop, min);
  56.         }    
  57.                 
  58.     } while( sortSize > 0);            //  1 = cool picts; 0 = actual sort
  59. }
  60.  
  61.  
  62.  
  63.